package Internect;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserManager {

        // 用户登录
        public void login(String username, String password) {
            if(username.equals("admin")) {
                if(password.equals("23456")) {
                    //System.out.println("");
                }
            } else {
                System.out.println("");
            }
        }

        // 用户注册
        public boolean register(String username, String password) {
            try {
                // 检查用户是否已存在
                File file = new File("user_info.txt");
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line = null;

                while ((line = reader.readLine()) != null) {
                    String[] parts = line.split(",");
                    if (parts[0].equals(username)) {
                        reader.close();
                        return false;
                    }
                }

                reader.close();

                // 添加用户信息
                FileWriter writer = new FileWriter(file, true);
                writer.write(username + "," + password + "\n");
                writer.close();

                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }
        }

        // 用户信息修改
        public boolean modify(String username, String password, String newUsername, String newPassword) {
            try {
                // 读取用户信息文件
                File file = new File("user_info.txt");
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line = null;
                List<String> lines = new ArrayList<String>();

                // 修改对应的用户信息
                while ((line = reader.readLine()) != null) {
                    String[] parts = line.split(",");
                    if (parts[0].equals(username) && parts[1].equals(password)) {
                        lines.add(newUsername + "," + newPassword);
                    } else {
                        lines.add(line);
                    }
                }

                reader.close();

                // 重新写入用户信息文件
                FileWriter writer = new FileWriter(file);
                for (String l : lines) {
                    writer.write(l + "\n");
                }
                writer.close();

                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }
        }

        // 用户退出
        public void logout() {
            System.exit(0);
        }


}
